package com.example.studyplanning.controller;

import com.example.studyplanning.entity.Like;
import com.example.studyplanning.services.LikeService;
import com.example.studyplanning.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("like")
public class LikeController {
    @Autowired
    private LikeService likeService;

    //点赞心得
    @PostMapping("/add")
    public Result InsertLike(@RequestBody Like like){
        Result result = new Result();
        int i =likeService.InsertLike(like);
        if(i==1){
            result.setCode(200);
            result.setMessage("点赞成功");
        }else{
            result.setCode(400);
            result.setMessage("点赞失败");
        }
        return result;
    };

    //展示点赞信息
    @GetMapping("/list")
    public Result FindLike(@RequestParam("userID") Integer userID,
                           @RequestParam("tipsID") Integer tipsID){
        Result result = new Result();
        List<Like> list = likeService.FindLike(userID,tipsID);
        if(list.size()==0){
            result.setCode(400);
            result.setMessage("未点赞");}
        else {
            result.setCode(200);
            result.setMessage("已点赞");
        }
        return result;
    };

    //取消点赞
    @GetMapping("/del")
    public Result DelLike(@RequestParam("userID") Integer userID,
                          @RequestParam("tipsID") Integer tipsID){
        Result result = new Result();
        int i = likeService.DelLike(userID,tipsID);
        if (i==1){
            result.setCode(200);
            result.setMessage("删除成功");
        }else {
            result.setCode(400);
            result.setMessage("删除失败");
        }
        return result;
    };
}
